<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册新用户</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            background: rgba(255, 255, 255, 0.9);
            padding: 30px;
            border-radius: 15px;
            box-shadow: 0 8px 20px rgba(0,0,0,0.1);
            width: 400px;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        .header h2 {
            color: #333;
            margin-bottom: 10px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-size: 14px;
        }
        .input-group {
            position: relative;
        }
        .input-group input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        .input-group input:focus {
            border-color: #4CAF50;
            box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
            outline: none;
        }
        .input-group input[readonly] {
            background-color: #f9f9f9;
            cursor: not-allowed;
        }
        .error-message {
            color: #e74c3c;
            font-size: 12px;
            margin-top: 5px;
        }
        .register-btn {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 20px;
        }
        .register-btn:hover {
            background-color: #45a049;
        }
        .success-message {
            display: none;
            text-align: center;
            color: #4CAF50;
            font-size: 16px;
            margin-top: 20px;
            padding: 15px;
            background-color: rgba(76, 175, 80, 0.1);
            border-radius: 8px;
        }
        .back-to-login {
            display: none;
            text-align: center;
            margin-top: 20px;
        }
        .back-to-login a {
            color: #4CAF50;
            text-decoration: none;
        }
        .back-to-login a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h2>注册新用户</h2>
        </div>
        <form id="registerForm">
            <div class="form-group">
                <label>您的账号</label>
                <div class="input-group">
                    <input type="text" id="username" readonly>
                </div>
            </div>
            <div class="form-group">
                <label>请输入密码</label>
                <div class="input-group">
                    <input type="password" id="password" required>
                </div>
                <div id="passwordLengthMessage" class="error-message"></div>
            </div>
            <div class="form-group">
                <label>请再次输入密码</label>
                <div class="input-group">
                    <input type="password" id="confirmPassword" required>
                </div>
                <div id="passwordMatchMessage" class="error-message"></div>
            </div>
            <button type="submit" class="register-btn">注册</button>
        </form>
        <div id="successMessage" class="success-message">
            注册成功！您的账号信息已保存。
        </div>
        <div class="back-to-login">
            <a href="/">返回登录页面</a>
        </div> 
    </div>

    <script>
        // 生成13位数字账号
        function generateUsername() {
            let username = '';
            for (let i = 0; i < 13; i++) {
                username += Math.floor(Math.random() * 10);
            }
            return username;
        }

        // 页面加载时生成账号
        window.onload = function() {
            document.getElementById('username').value = generateUsername();
        };

        // 检查密码长度
        function checkPasswordLength() {
            const password = document.getElementById('password').value;
            const messageDiv = document.getElementById('passwordLengthMessage');

            if (password.length > 0 && password.length < 6) {
                messageDiv.textContent = '密码过短，不安全';
                return false;
            } else {
                messageDiv.textContent = '';
                return true;
            }
        }

        // 检查密码是否匹配
        function checkPasswordMatch() {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;
            const messageDiv = document.getElementById('passwordMatchMessage');

            if (password === '' || confirmPassword === '') {
                messageDiv.textContent = '';
                return false;
            }

            if (password === confirmPassword) {
                messageDiv.textContent = '';
                return true;
            } else {
                messageDiv.textContent = '两次密码输入不一样';
                return false;
            }
        }

        // 实时检查密码长度和匹配
        document.getElementById('password').addEventListener('input', function() {
            checkPasswordLength();
            checkPasswordMatch();
        });
        document.getElementById('confirmPassword').addEventListener('input', checkPasswordMatch);

        // 表单提交处理
        document.getElementById('registerForm').addEventListener('submit', function(event) {
            event.preventDefault();
            if (!checkPasswordLength() || !checkPasswordMatch()) {
                return;
            }

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 获取已存在的用户数据
            let config = JSON.parse(localStorage.getItem('config') || '{"users":[]}');

            // 检查用户名是否已存在
            if (config.users.some(user => user.username === username)) {
                alert('该账号已存在！');
                return;
            }

            // 添加新用户
            config.users.push({
                username: username,
                password: password
            });

            // 保存到localStorage
            localStorage.setItem('config', JSON.stringify(config));

            // 隐藏注册表单
            document.getElementById('registerForm').style.display = 'none';
            // 显示成功消息
            document.getElementById('successMessage').style.display = 'block';
            // 显示返回登录链接
            document.querySelector('.back-to-login').style.display = 'block';
        });
    </script>
</body>
</html>
